﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Controller;

namespace Temp.View
{
    class DescriptorView
    {
        Texture2D descBox;
        SpriteFont myFont;
        MouseState m; 
        Vector2 dimX, dimY; 
        String curDesc;
        ObjectController objCtr;
        bool show;
        int timer, mouseTimer;
        bool b;
        Random rand;
        List<String> descs;
        String curObj;
        private int curPlace;
        public bool invShowing { get; set; }
        
        public DescriptorView()
        {
            curDesc = "";
            curObj = "";
            objCtr = new ObjectController();  
            show = false;
            timer = 0;
            mouseTimer = 0;
            b = false;
            rand = new Random();
            descs = new List<string>();
        }

        public void LoadContent(ContentManager content)
        {
            descBox = content.Load<Texture2D>("UI elements/descBox");
            myFont = content.Load<SpriteFont>("MyFont");
        }

        public void Update(GameTime gameTime)
        { 
                processMouse();  
        }

        public void processMouse()
        {
            String oldObj = curObj;
            m = Mouse.GetState();
            
            if (m.LeftButton == ButtonState.Pressed && (timer>50 || timer==0))
            {
                
                foreach (Temp.Model.Object obj in objCtr.getObjectsInPlace(curPlace))
                {
                    if (m.X >= obj.x && m.X <= obj.x + obj.dimX && m.Y >= obj.y && m.Y <= obj.y + obj.dimY)
                    {
  
                            b = true; 
                            descs = objCtr.getDescription(obj.name);
                            mouseTimer = 0;
                            timer = 0;

                            try
                            {
                                curObj = obj.name;
                                curDesc = descs[rand.Next(descs.Count)];
                                break;

                            }
                            catch (Exception ex)
                            {
                                curDesc = "Error: " + obj.name + " has no definition in database";
                            }
                            break;
                    }
                    else 
                    {
                        show = false;
                        mouseTimer = 0;
                        b = false;
                    }

                }
            
                dimX = new Vector2(m.X, m.Y);

            } 
            if (b)
            { 
                mouseTimer++;
                timer++;

                if (mouseTimer <= 300)
                    show = true;
                else
                {
                    show = false;
                    mouseTimer = 0;
                    b = false;
                }
            }
            
                /*
                foreach (Temp.Model.Object obj in objCtr.getObjectsInPlace(3))
                {
                    System.Diagnostics.Debug.WriteLine(obj.name);
                }*/
            
            if (m.RightButton == ButtonState.Pressed)
                dimY = new Vector2(m.X, m.Y);
          
        }

        public void Draw(SpriteBatch spriteBatch)
        {

            //spriteBatch.DrawString(myFont, "x1:" + dimX.X + "     y1:" + dimX.Y, new Vector2(50, 100), Color.DarkRed);
            //spriteBatch.DrawString(myFont, "x2:" + dimY.X + "    y2:" + dimY.Y, new Vector2(50, 150), Color.DarkRed);
            //spriteBatch.DrawString(myFont, "dimX:" + (dimY.X - dimX.X) + "     dimY:" + (dimY.Y - dimX.Y), new Vector2(50, 200), Color.DarkRed);
            //spriteBatch.DrawString(myFont, mouseTimer + "", new Vector2(50, 250), Color.Yellow);
            
            
            if (show && !invShowing)
            {
                spriteBatch.Draw(descBox, new Rectangle(3, 729, 1360, 40), Color.White); 
                spriteBatch.DrawString(myFont, curDesc, new Vector2(15, 732), new Color(244, 244, 244));
                //if(mouseTimer<=200)
                  //  spriteBatch.DrawString(myFont, curObj, new Vector2(m.X-30, m.Y-30), Color.Snow);
            }
        }

        public void SetDesc(String desc)
        {
            curDesc = desc;
        }

        public void Show(bool b)
        {
            show = b;
        }
        /// <summary>
        /// just update the current place & everything will follow
        /// </summary>
        public int CurPlace
        {
            get { return curPlace; }
            set { curPlace = value; }
        }
    }
}
